﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.Mtbl;
namespace WebApplication
{
    public partial class Businessnature : BasePage
    {
        private int customeruid = 0;
        tblCust_BusNature cust_busNatureENT = new tblCust_BusNature();
        tblCust_BusNatureBLL cust_busNatureBLL = new tblCust_BusNatureBLL();
        mtblBusinessNature busNatureENT = new mtblBusinessNature();
        mtblBusinessNatureBLL busNatureBLL = new mtblBusinessNatureBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindNatureList();
                if (Request.QueryString[Constant.UrlParamName.CustomerUID] != null)
                {
                    if (int.TryParse(Request.QueryString[Constant.UrlParamName.CustomerUID], out customeruid))
                    {
                        SetCustomerNature();
                    }
                    else
                    {
                        SetNoOption();
                    }
                }
                else
                {
                    SetNoOption();
                    lblMeg.Text = ResourceManage.GetResourceString("msg200002");
                }
            }
        }
        private void SetNoOption()
        {
            lkBtnSave.Enabled = false;
            lblMeg.Text = ResourceManage.GetResourceString("msg200001");
        }
        private void BindNatureList()
        {            
            busNatureENT.Deleted = 0;
            var list = busNatureBLL.GetListByENT(busNatureENT);
            rptNatureList.DataSource = list;
            //ckbNatureList.DataSource = list;
            //ckbNatureList.DataTextField = "BusNature_Chi";
            //ckbNatureList.DataValueField = "BusNatureUID";
            rptNatureList.DataBind();
        }
        private void SetCustomerNature()
        {
            if (customeruid > 0)
            {
                tblCustomer customerENT = new tblCustomer();
                tblCustomerBLL customerBLL = new tblCustomerBLL();
                customerENT.CustomerUID = customeruid;
                customerENT.Deleted = 0;
                customerENT = customerBLL.GetEntity(customerENT);
                if (customerENT != null)
                {
                    cust_busNatureENT.Deleted = 0;
                    cust_busNatureENT.CustomerUID = customeruid;
                    var list = cust_busNatureBLL.GetListByENT(cust_busNatureENT, true);
                    string strbusNatureUID = string.Empty;
                    if (list.Count > 0)
                    {
                        foreach (var tempENT in list)
                        {
                            strbusNatureUID += tempENT.BusNatureUID.ToString() + ",";
                            //for (int i = 0; i < ckbNatureList.Items.Count; i++ )
                            //{
                            //    if (ckbNatureList.Items[i].Value == tempENT.BusNatureUID.ToString())
                            //    {
                            //        ckbNatureList.Items[i].Selected = true;
                            //        break;
                            //    }
                            //}
                        }
                        hidStrNatureUID.Value = strbusNatureUID;
                    }
                }
                else
                {
                    SetNoOption();
                    lblMeg.Text = ResourceManage.GetResourceString("msg200014");
                }
            }
        }       

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool ret =false;
            if (int.TryParse(Request.QueryString[Constant.UrlParamName.CustomerUID], out customeruid))
            {
                //校验CustomerUID是否存在
                tblCustomer customerENT = new tblCustomer();
                tblCustomerBLL customerBLL = new tblCustomerBLL();
                customerENT.CustomerUID = customeruid;
                var tempENT = customerBLL.GetEntity(customerENT);
                if (tempENT != null)
                {
                    string LastModifyBy = Common.GetManageName();
                    List<int> AddList = new List<int>();
                    List<int> DelList = new List<int>();
                    AddList = GetAddList();
                    DelList = GetDelList();
                    WriteToLog();//写log
                    ret = cust_busNatureBLL.SaveList(AddList, DelList, customeruid, LastModifyBy);
                    if (ret)
                    {
                        lblMeg.Text = ResourceManage.GetResourceString("msg200004");
                        SetCustomerNature();
                    }
                    else
                    { lblMeg.Text = ResourceManage.GetResourceString("msg200003"); }
                }
                else
                {
                    SetNoOption();
                    lblMeg.Text = ResourceManage.GetResourceString("msg200014");
                }
            }
            else
            {
                SetNoOption();
            }
            
        }

        protected List<int> GetAddList()
        {
            return GetList(hidNeedToAdd);
        }
        protected List<int> GetDelList()
        {
            return GetList(hidNeedToDel);
        }

        protected List<int> GetList(HiddenField name)
        {
            List<int> ret = new List<int>();
            if (!string.IsNullOrEmpty(name.Value))
            {
                ret = name.Value.StringToList();
            }
            return ret;
        }

        protected void WriteToLog()
        {
            tblUser ent = GetManageFromSession();
            customeruid =Convert.ToInt32(Request.QueryString[Constant.UrlParamName.CustomerUID]);
            
            string addlog = string.Empty;
            string dellog = string.Empty;
            mtblBusinessNature entBN = new mtblBusinessNature();
            List<mtblBusinessNature> list = mtblBusinessNatureBLL.GetInstance().GetListByENT(entBN);
            string[] add = hidNeedToAdd.Value.Split(',');
            string[] delete = hidNeedToDel.Value.Split(',');
            if (add.Length > 1 | delete.Length > 1)
            {
                for (int i = 0; i < add.Length - 1; i++)
                {
                    var result = list.Where(a => a.BusNatureUID == Convert.ToInt32(add[i])).Select(p => p).ToList();
                    foreach (mtblBusinessNature item in result)
                    {
                        addlog += item.BusNature_Eng + ",";
                    }
                }
                for (int i = 0; i < delete.Length - 1; i++)
                {
                    var result = list.Where(a => a.BusNatureUID == Convert.ToInt32(delete[i])).Select(p => p).ToList();
                    foreach (mtblBusinessNature item in result)
                    {
                        dellog += item.BusNature_Eng + ",";
                    }
                }
                if (addlog.Length > 1)
                    addlog = " Add " + addlog.Substring(0, addlog.Length - 1);
                else
                    addlog = "";
                if (dellog.Length > 1)
                    dellog = " Delete " + dellog.Substring(0, dellog.Length - 1);
                else
                    dellog = "";

                Literal CompanyName = usermeg.FindControl("ltlCompanyName_eng") as Literal;
                tblHistory entlog = new tblHistory();
                entlog.HistoryCatUID = 2;
                entlog.HistoryTypeUID = 8;
                entlog.CustomerUID = customeruid;
                entlog.Cust_MemberUID = 0;
                entlog.ContactPersonUID = 0;
                entlog.History_Detail = ent.User_Name + " {"+CompanyName.Text+"}{BusinessNature}" + addlog + dellog;
                entlog.LastModifyBy = ent.User_Name;
                int tal = tblHistoryBLL.GetInstance().InsertENT(entlog);
            }
        }
    }
}